<style>
.app-main{
  background: #f5f9ff;
}
</style>
<template>
  <div class="app-container" style="margin: auto; margin-top: 20px; border-radius:5px; width: 80%; height: 700px; background: #FFFFFF">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-button type="primary" @click="tiao" plain>开票</el-button>
      <el-button plain>导入</el-button>
      <el-button plain @click="handleExport">导出</el-button>
      <el-button @click="handlePrint" plain>打印</el-button>
      <br><br>
      <el-form-item label="订单状态">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择" clearable>
          <el-option label="全部" value="全部"></el-option>
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="待调度" value="待调度"></el-option>
          <el-option label="运输中" value="运输中"></el-option>
          <el-option label="已签收" value="已签收"></el-option>
          <el-option label="已拒绝" value="已拒绝"></el-option>
          <el-option label="已取消" value="已取消"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="签收时间">
        <el-date-picker
          v-model="daterangeSignTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="开票时间">
        <el-date-picker
          v-model="daterangeInvoicerTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['fapiao:fapiao:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['fapiao:fapiao:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['fapiao:fapiao:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['fapiao:fapiao:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="fapiaoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="流水单号" align="center" prop="serialNumber" />
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="客户" align="center" prop="customerName" />
      <el-table-column label="客户编码" align="center" prop="customerCode" />
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <span>{{scope.row.orderStatus==0?'待审核':scope.row.orderStatus==1?'待调度':scope.row.orderStatus==2?'运输中':scope.row.orderStatus==3?'已签收':scope.row.orderStatus==4?'已拒绝':'已取消'}}</span>
        </template>
      </el-table-column>>
      <el-table-column label="签收时间" align="center" prop="signTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货地址" align="center" prop="shippingAddress" />
      <el-table-column label="收货地址" align="center" prop="receivingAddress" />
      <el-table-column label="结算状态" align="center" prop="settlementStatus" >
        <template slot-scope="scope">
          <span>{{scope.row.settlementStatus==0?'未结算':scope.row.settlementStatus==1?'已结算':'部分结算'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算时间" align="center" prop="settlementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账单状态" align="center" prop="reconciliationStatus" />
      <el-table-column label="对账单号" align="center" prop="reconciliationNumber" />
      <el-table-column label="开票状态" align="center" prop="invoicingStatus" >
        <template slot-scope="scope">
          <span>{{scope.row.invoicingStatus==0?'未开票':'已开票'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="开票金额" align="center" prop="invoicingAmount" />
      <el-table-column label="开票人" align="center" prop="invoicer" />
      <el-table-column label="开票时间" align="center" prop="invoicerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoicerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流水单号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入流水单号" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="客户" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="客户编码" prop="customerCode">
          <el-input v-model="form.customerCode" placeholder="请输入客户编码" />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="签收时间" prop="signTime">
          <el-date-picker clearable
            v-model="form.signTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签收时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货地址" prop="shippingAddress">
          <el-input v-model="form.shippingAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="收货地址" prop="receivingAddress">
          <el-input v-model="form.receivingAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="结算时间" prop="settlementTime">
          <el-date-picker clearable
            v-model="form.settlementTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结算时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="对账单号" prop="reconciliationNumber">
          <el-input v-model="form.reconciliationNumber" placeholder="请输入对账单号" />
        </el-form-item>
        <el-form-item label="开票金额" prop="invoicingAmount">
          <el-input v-model="form.invoicingAmount" placeholder="请输入开票金额" />
        </el-form-item>
        <el-form-item label="开票人" prop="invoicer">
          <el-input v-model="form.invoicer" placeholder="请输入开票人" />
        </el-form-item>
        <el-form-item label="开票时间" prop="invoicerTime">
          <el-date-picker clearable
            v-model="form.invoicerTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开票时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFapiao, getFapiao, delFapiao, addFapiao, updateFapiao } from "@/api/fapiao/fapiao";

export default {
  name: "Fapiao",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票管理表格数据
      fapiaoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 开票时间时间范围
      daterangeSignTime: [],
      // 开票时间时间范围
      daterangeInvoicerTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderStatus: null,
        signTime: null,
        invoicerTime: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handlePrint() {
      // 调用 window.print() 方法进行打印
      window.print();
    },
    tiao(){
      if(this.ids.length==0){
        this.$message.error("请选着数据");
      }else {
        if (this.ids.length > 5) {
          this.$message.error("只能选着5条数据");
        } else {
          this.$router.push({path: '/addf', query: {ids: this.ids}})
        }
      }
    },
    /** 查询发票管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSignTime && '' != this.daterangeSignTime) {
        this.queryParams.params["beginSignTime"] = this.daterangeSignTime[0];
        this.queryParams.params["endSignTime"] = this.daterangeSignTime[1];
      }
      if (null != this.daterangeInvoicerTime && '' != this.daterangeInvoicerTime) {
        this.queryParams.params["beginInvoicerTime"] = this.daterangeInvoicerTime[0];
        this.queryParams.params["endInvoicerTime"] = this.daterangeInvoicerTime[1];
      }
      listFapiao(this.queryParams).then(response => {
        this.fapiaoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNumber: null,
        orderNumber: null,
        customerName: null,
        customerCode: null,
        contractName: null,
        orderStatus: null,
        signTime: null,
        shippingAddress: null,
        receivingAddress: null,
        settlementStatus: null,
        settlementTime: null,
        reconciliationStatus: null,
        reconciliationNumber: null,
        invoicingStatus: null,
        invoicingAmount: null,
        invoicer: null,
        invoicerTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSignTime = [];
      this.daterangeInvoicerTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFapiao(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFapiao(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFapiao(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除发票管理编号为"' + ids + '"的数据项？').then(function() {
        return delFapiao(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fapiao/fapiao/export', {
        ...this.queryParams
      }, `fapiao_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
